/*
 * author: Andriamananony, Meß, Falk
 */

package util;

public class Interval implements Comparable<Interval> {

	private int nStart;
	private int nEnd;
	
	public Interval(int s, int e){
		if(s <= e){
			nStart = s;
			nEnd = e;
		}
		else
			throw new IllegalArgumentException();
	}
	
	public boolean overlapsWith(Interval i){
		if(nStart <= i.nStart && i.nStart < nEnd)
			return true;
		else if(nStart < i.nEnd && i.nEnd <= nEnd)
			return true;
		else if(i.nStart <= nStart && i.nEnd >= nEnd)
			return true;
		return false;
	}
	
	public int getEnd(){
		return nEnd;
	}
	
	public int getStart(){
		return nStart;
	}
	
	public void moveTo(int i){
		nEnd = i+getDuration();
		nStart = i;
	}
	
	public void moveBy(int i){
		nEnd += i;
		nStart += i;
	}
	
	public int getDuration(){
		return nEnd-nStart;
	}
	
	@Override
	public String toString(){
		return nStart+"-"+nEnd;
	}

	/**
	 * i1.compareTo(i2) returns -1 iff i1 is earlier than i2. 
	 */
	@Override
	public int compareTo(Interval i) {
		if(nStart < i.nStart)
			return -1;
		if(nStart > i.nStart)
			return 1;
		if(nEnd < i.nEnd)
			return -1;
		if(nEnd > i.nEnd)
			return 1;
		return 0;
	}
	
	@Override
	public boolean equals(Object o){
		if(Interval.class.isInstance(o)){
			Interval i = (Interval)o;
			return i.nStart == nStart && i.nEnd == nEnd;
		}
		return false;
	}

}
